package main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import main.ChubJob;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;


public class ChubScheduler {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		try { 

			   Properties prop=new Properties();
			   System.out.println(ChubJob.getJarDirectory()+File.separator+"scheduler_conf.properties");
			   prop.load(new FileInputStream(ChubJob.getJarDirectory()+File.separator+"scheduler_conf.properties"));
			   String chub_job_active = prop.getProperty("chub_job_active");
			   String archive_job_active = prop.getProperty("archive_job_active");
			   
			   String interval_in_milliseconds = prop.getProperty("run_interval_in_milliseconds");
			   Long interval_in_milliseconds_long = Long.valueOf(interval_in_milliseconds);
			   
			    String archive_interval_in_milliseconds = prop.getProperty("archive_run_interval_in_milliseconds");
				Long archive_interval_in_milliseconds_long = Long.valueOf(archive_interval_in_milliseconds);
				
				SchedulerFactory sf=new StdSchedulerFactory();
				Scheduler sched = sf.getScheduler();
				sched.start();
				
			   //job1
			   if(chub_job_active!=null && chub_job_active.trim().equals("true"))
			   {
					JobDetail jd=new JobDetail("chub_job",Scheduler.DEFAULT_GROUP,ChubJob.class);
				    SimpleTrigger st=new SimpleTrigger("chub_trigger",Scheduler.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY, interval_in_milliseconds_long);// *
				    sched.scheduleJob(jd, st);
			   }
			   //job2
			   if(archive_job_active!=null && archive_job_active.trim().equals("true"))
				{
					JobDetail ajd=new JobDetail("archive_job",Scheduler.DEFAULT_GROUP,ArchiveJob.class);
				    SimpleTrigger ast=new SimpleTrigger("archive_trigger",Scheduler.DEFAULT_GROUP,new Date(),null,SimpleTrigger.REPEAT_INDEFINITELY, archive_interval_in_milliseconds_long);// *
				    sched.scheduleJob(ajd, ast);
			    }
				  
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
		
		
	}

}
